package myta.bean;

import java.util.Map;

import javax.faces.component.html.HtmlInputText;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class AdminData extends FormDataBase{
	private HtmlInputText txtidadmin;
	private HtmlInputText txtnama;
	private HtmlInputText txtalamat;
	private HtmlInputText txttelepon;
	private HtmlInputText txtusername;
	private HtmlInputText txtpassword;
	
	@Override
	public void init() throws Exception {
		formName = "admin";
		
		txtidadmin = new HtmlInputText();
		txtnama = new HtmlInputText();
		txtalamat = new HtmlInputText();
		txttelepon = new HtmlInputText();
		txtusername = new HtmlInputText();
		txtpassword = new HtmlInputText();
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idadmin()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtidadmin.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idadmin")){
			String idadmin = map.get("idadmin");
			
			String sql = "select * from admin where idadmin = ? order by idadmin desc";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idadmin);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtidadmin.setValue(datarow.getString("idadmin"));
			txtnama.setValue(datarow.getString("nama"));
			txtalamat.setValue(datarow.getString("alamat"));
			txttelepon.setValue(datarow.getString("telepon"));
			txtusername.setValue(datarow.getString("username"));
			txtpassword.setValue(datarow.getString("passwd"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO admin(idadmin,nama,alamat,telepon,username,passwd) VALUES(?,?,?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtidadmin.getValue().toString());
		command.setObject(2, txtnama.getValue().toString());
		command.setObject(3, txtalamat.getValue().toString());
		command.setObject(4, txttelepon.getValue().toString());
		command.setObject(5, txtusername.getValue().toString());
		command.setObject(6, txtpassword.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE admin SET nama=?,alamat=?,telepon=?,username=?,passwd=? where idadmin = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnama.getValue().toString());
		command.setObject(2, txtalamat.getValue().toString());
		command.setObject(3, txttelepon.getValue().toString());
		command.setObject(4, txtusername.getValue().toString());
		command.setObject(5, txtpassword.getValue().toString());
		command.setObject(6, txtidadmin.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtidadmin() {
		return txtidadmin;
	}

	public void setTxtidadmin(HtmlInputText txtidadmin) {
		this.txtidadmin = txtidadmin;
	}

	public HtmlInputText getTxtnama() {
		return txtnama;
	}

	public void setTxtnama(HtmlInputText txtnama) {
		this.txtnama = txtnama;
	}

	public HtmlInputText getTxtalamat() {
		return txtalamat;
	}

	public void setTxtalamat(HtmlInputText txtalamat) {
		this.txtalamat = txtalamat;
	}

	public HtmlInputText getTxttelepon() {
		return txttelepon;
	}

	public void setTxttelepon(HtmlInputText txttelepon) {
		this.txttelepon = txttelepon;
	}

	public HtmlInputText getTxtusername() {
		return txtusername;
	}

	public void setTxtusername(HtmlInputText txtusername) {
		this.txtusername = txtusername;
	}

	public HtmlInputText getTxtpassword() {
		return txtpassword;
	}

	public void setTxtpassword(HtmlInputText txtpassword) {
		this.txtpassword = txtpassword;
	}
}
